package com.jiaai.cloud.live.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.jiaai.cloud.base.exception.ServiceException;
import com.jiaai.cloud.base.page.FrontPage;
import com.jiaai.cloud.base.util.RestResultGenerator;
import com.jiaai.cloud.live.entity.TAnchorTask;
import com.jiaai.cloud.live.entity.TAnchorTaskSearch;
import com.jiaai.cloud.live.service.ITAnchorTaskService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 主播任务列表 前端控制器
 * </p>
 *
 * @author 佳爱网络科技
 * @since 2017-08-17
 */
@RestController
@RequestMapping("api/manage/tAnchorTask")
public class TAnchorTaskController {
    @Autowired
    private ITAnchorTaskService itAnchorTaskService;

    //可以根据Id查找,不传id查所有
    @RequestMapping(value = "/findByAll", method = RequestMethod.GET, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object findByAll(TAnchorTaskSearch search) throws ServiceException {
        try {
            EntityWrapper wrapper=new EntityWrapper();
            if(!StringUtils.isEmpty(search.getAnchorId())){
                wrapper.like("anchorId",search.getAnchorId());
            }
            if(!StringUtils.isEmpty((search.getTaskName()))){
                wrapper.like("taskName",search.getTaskName());
            }
            return RestResultGenerator.genSuccessResult(itAnchorTaskService.findTaskFully(search.getPagePlus(),wrapper));
        } catch (Exception e) {
            throw new ServiceException("查询失敗", e.getMessage());
        }
    }

    //通过改id改status状态
    @RequestMapping(value = "/changeStatus/{id}/{status}", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public Object changeStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) throws ServiceException {
        try {
            TAnchorTask tAnchorTask = new TAnchorTask();
            tAnchorTask.setId(id);
            tAnchorTask.setStatus(status);
            itAnchorTaskService.updateById(tAnchorTask);
            return RestResultGenerator.genSuccessResult();
        }catch (Exception e) {
            throw new ServiceException("修改失敗", e.getMessage());
        }
    }
}
